package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.utils.AliOSSProperties;
import com.itheima.entity.Result;
import com.itheima.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile file) throws Exception {
//        log.info("参数：{},{},{}",username,age,file);
//        String originalFilename = file.getOriginalFilename();
//        String newFileName= UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("D:/javaweb/HeiMaStudy/TestReserver/"+newFileName));
//        return Result.success(newFileName);
//    }

//    private final String bucketName="java-147tlias";
//    private final String endpoint="https://oss-cn-shenzhen.aliyuncs.com";

//    @Value("${aliyun.oss.bucketName}")
//    private  String bucketName;
//    @Value("${aliyun.oss.endpoint}")
//    private  String endpoint;

    @Autowired
    private AliOSSProperties aliOSSProperties;//获取阿里云OSS配置信息
    @Log
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        //1.获取原始文件名
        String originalFilename = file.getOriginalFilename();//获取原始文件名
        log.info("原始文件名：{}",originalFilename);
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件后缀名
        //2.调用阿里云OSS存储服务进行上传
        String url = AliyunOSSUtils.upload(aliOSSProperties.getEndpoint(), aliOSSProperties.getBucketName(), file.getBytes(), extName);
        //3.返回结果
        return Result.success(url);
    }
}
